package com.lucky.demo.controller;

import cn.idev.excel.FastExcel;
import com.lucky.demo.listener.ExcelListenner;
import com.lucky.demo.pojo.vo.DemoExcelVO;
import com.lucky.common.base.BaseController;
import com.lucky.common.util.ExcelUtil;
import javax.servlet.ServletOutputStream;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/demo")
public class DemoExcelController extends BaseController {

	/**
	 * 读取excel
	 * @param demoFile
	 * @return
	 */
	@PostMapping("/xls")
	public ResponseEntity<String> upload(@RequestParam("demoFile") MultipartFile demoFile) {
		if (demoFile.isEmpty()) {
			return ResponseEntity.badRequest().body("请上传一个有效的文件！");
		}
		try {
			ExcelListenner<DemoExcelVO> demoListener = new ExcelListenner<>();
			FastExcel.read(demoFile.getInputStream(), DemoExcelVO.class, demoListener).sheet().doRead();
			List<DemoExcelVO> dataList = demoListener.getDataList();
			System.out.println(dataList);
			dataList.forEach(data -> {
				//处理数据
			});

			return ResponseEntity.ok("员工数据文件上传并处理成功！");
		} catch (IOException e) {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件处理失败！");
		}
	}

	/**
	导出excel
	 */
	@GetMapping("/xls")
	public ResponseEntity<byte[]> xls(HttpServletResponse response) throws IOException {
		DemoExcelVO vo1 = new DemoExcelVO(1, "张三", 18);
		DemoExcelVO vo2 = new DemoExcelVO(2, "李四", 30);
		DemoExcelVO vo3 = new DemoExcelVO(2, "王五", 30);
		List<DemoExcelVO> dataList = Arrays.asList(vo1, vo2, vo3);

		// 2. 使用 FastExcel 写入数据到字节流
		byte[] data = ExcelUtil.toExcel( DemoExcelVO.class, dataList, "demo");
		return ExcelUtil.download(data, "demo.xls");
	}

	/**
	 * 另一种方式：直接通过 HttpServletResponse 输出（可选方案）
	 */
	@GetMapping("/xls2")
	public void exportExcel2(HttpServletResponse response) throws IOException {

		// 准备数据
		DemoExcelVO vo1 = new DemoExcelVO(1, "张三", 18);
		DemoExcelVO vo2 = new DemoExcelVO(2, "李四", 30);
		DemoExcelVO vo3 = new DemoExcelVO(2, "王五", 30);
		List<DemoExcelVO> dataList = Arrays.asList(vo1, vo2, vo3);


		// 设置响应头
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		String fileName = URLEncoder.encode("用户列表.xls", StandardCharsets.UTF_8);
		response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName);


		// 直接写入响应输出流
		FastExcel.write(response.getOutputStream(), DemoExcelVO.class)
				.sheet("用户列表")
				.doWrite(dataList);
	}

}
